﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DI;
using Front.Controllers.Api;
using Core.Dtos;
using Core.Messages;
using StructureMap;
using Core.Queries;
using StructureMap.AutoMocking;
using System.Transactions;
using Tests.Helpers;
using StructureMap.Pipeline;
using Insurance.Repositories;
using Core.Workflows;

namespace Insurance.Test
{
    [TestClass]
    public class PolicyControllerTest
    {
        private TransactionScope transScope;
        protected ControllerHelper<PolicyApiController> controllerHelper;

        [ClassInitialize()]
        void Initialize()
        {            
        }

        [TestInitialize()]
        public void InitializeTest()
        {
            Bootstrapper.Init(new SingletonLifecycle());
            CurrentContextHelper.Logon();
            transScope = new TransactionScope(TransactionScopeOption.Required);            
            controllerHelper = new ControllerHelper<PolicyApiController>();
            controllerHelper.Initialize();
        }

        [TestCleanup()]
        public void CleanupTest()
        {
            controllerHelper.Cleanup();
            Bootstrapper.Resolve<OfficeDbContext>().Dispose();
            transScope.Dispose();            
        }

        [TestMethod]
        public void PolicyApiController_CanCreate()
        {
            // Arrange            
            var controller = controllerHelper.GetController();

            // Act            
            var response = controller.Post(GetSampleDto());

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Create failed");            
        }

        [TestMethod]
        public void PolicyApiController_CanRead()
        {
            // Arrange            
            var controller = controllerHelper.GetController();
            var id = controller.Post(GetSampleDto()).Value;

            // Act            
            var dto = (MotorPolicyDto)controller.Get(id);

            // Assert
            Assert.IsNotNull(dto, "Get failed");        
        }

        [TestMethod]
        public void PolicyApiController_CanEdit()
        {
            // Arrange            
            var controller = controllerHelper.GetController();
            var dto = GetSampleDto();
            dto.Id = controller.Post(dto).Value;

            // Act            
            dto.StateID = (int)ApprovalState.Pending;
            dto.Number = 98765;
            Response response = controller.Put(dto.Id, dto);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Edit failed");
        }

        [TestMethod]
        public void PolicyApiController_CanDelete()
        {
            // Arrange      
            var controller = controllerHelper.GetController();
            var id = controller.Post(GetSampleDto()).Value;

            // Act
            Response response = controller.Delete(id);

            //Assert            
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Delete failed");
        }

        [TestMethod]
        public void PolicyApiController_CanFindBy()
        {
            // Arrange            
            var controller = controllerHelper.GetController();
            controller.Post(GetSampleDto());
            var query = new PolicyQuery() { Number = 12345 };            

            // Act            
            var response = controller.FindBy(query);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(response.List.Count, 1, "FindBy Failed");           
        }

        public MotorPolicyDto GetSampleDto()
        {
            var policy = new MotorPolicyDto() { Number = 12345, PlateNo = "B12345", TypeOfFuelID = 1 };
            policy.Person = new PersonDto() { Name = "Person1" };
            return policy;
        }
    }
}
